package com.googlecode.cswish.struts.conversion;

import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.util.StrutsTypeConverter;

public class BooleanConverter extends StrutsTypeConverter {

	private static final Logger logger = Logger.getLogger(DefaultDateConverter.class);

	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {  
		String sBoolean = null;
		Boolean ret = Boolean.FALSE;
		if (values != null && values.length > 0) {
			sBoolean = values[0];  
			if (sBoolean != null && sBoolean.length() > 0) {
				
				if (sBoolean.equals("Y") || sBoolean.equals("y") 
						|| sBoolean.equals("TRUE") || sBoolean.equals("true")) {
					ret = Boolean.TRUE;
				}
			}
		}
		return ret;
	}

	@Override
	public String convertToString(Map context, Object o) {
		String val = "N";
		if (o != null) {
			if (o instanceof Boolean) {
				Boolean ret = (Boolean) o;
				if (ret) {
					val = "Y";
				}
			}
		}
		return val;
	}
	
}